package com.itheima.CSM_bacK.aabServices;

import com.itheima.CSM_bacK.aabServices.Serv.OrderSettingServ;
import com.itheima.CSM_bacK.aacMapper.OrderSettingMapper;
import com.itheima.pojo.Entity.OrderSetting;
import com.itheima.pojo.DTO.DTOoSettingEdit;
import com.itheima.pojo.VO.VOoSettingGet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderSettingImpl implements OrderSettingServ {
    @Autowired
    private OrderSettingMapper orderSettingMapper;


    @Override
    public void editNumberByOrderDate(DTOoSettingEdit kk) {

        String ss = kk.getOrderDate().toString();
        String[] sp = ss.split("-");
        int year = Integer.parseInt(sp[0]);
        int month = Integer.parseInt(sp[1]);
        int day = Integer.parseInt(sp[2]);
        LocalDate localDate = LocalDate.of(year, month, day);
        OrderSetting orderSetting = orderSettingMapper.findByOrderDate(localDate);

        if (orderSetting == null) {
            orderSetting = new OrderSetting();
            orderSetting.setNumber(kk.getNumber());
            orderSetting.setOrderDate(localDate);
            orderSettingMapper.addNumberByOrderDate(orderSetting);
        } else {
            if (orderSetting.getNumber()!=kk.getNumber()) {
                orderSetting.setNumber(kk.getNumber());
                orderSettingMapper.editNumberByOrderDate(orderSetting);
            }
        }

    }

    @Override
    public List<VOoSettingGet> getOrderSettingByMonth(String month) {
        String[] split = month.split("-");
        int year = Integer.parseInt(split[0]);
        int months = Integer.parseInt(split[1]);
        YearMonth yearMonth = YearMonth.of(year, months);
        LocalDate lastDay = yearMonth.atEndOfMonth();
        LocalDate firstDay = LocalDate.of(year, months, 1);
        List<OrderSetting> kks = orderSettingMapper.getOrderSettingByMonth(firstDay,lastDay);
        List<VOoSettingGet> vooSettingGets = new ArrayList<>();
        for (OrderSetting kk : kks) {
            VOoSettingGet vooSettingGet = new VOoSettingGet();
            vooSettingGet.setDate(kk.getOrderDate().toString().split("-")[2]);
            vooSettingGet.setNumber(kk.getNumber());
            vooSettingGet.setReservations(kk.getReservations());
            vooSettingGets.add(vooSettingGet);
        }
        return vooSettingGets;
    }
}
